package demo4;
//比较对象

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {

        //return this.age - o.age;
        //return this.score - o.score;

        //这里就进入String类中的compareTo
       if(this.name.compareTo(o.name) > 0) {
            return 1;
        }else if(this.name.compareTo(o.name) < 0) {
            return -1;
        }else {
            return 0;
        }
    }
}


//比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class ScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.score - o2.score;
    }
}

class NameComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class TestDemo4 {

    public static void main4(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhagnsan",10,19);
        students[1] = new Student("abc",8,78);
        students[2] = new Student("lisi",15,57);

        //比较器
        AgeComparator ageComparator = new AgeComparator();

        ScoreComparator scoreComparator = new ScoreComparator();

        NameComparator nameComparator = new NameComparator();

        Arrays.sort(students, ageComparator);

        System.out.println(Arrays.toString(students));


    }

    //传参过来，向上转型
    //模拟Arrays.sort()
    public static void sort(Comparable[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                /*if(array[j] > array[j+1]) {
                    交换；
                }*/
                if(array[j].compareTo(array[j+1]) > 0) {
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }
    
    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",19,10);
        students[1] = new Student("lisi",59,20);
        students[2] = new Student("abc",39,5);

        //System.out.println(students[0].compareTo(students[1]));

        sort(students);

        System.out.println(Arrays.toString(students));
    }

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhagnsan",10,19);
        students[1] = new Student("abc",8,78);
        students[2] = new Student("lisi",15,57);
        /*if(students[0].compareTo(students[1]) > 0) {
            System.out.println("students[0] > students[0]");
        }*/
        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
    }

    public static void main1(String[] args) {
        int[] array = {1,3,2,10,5,3,7};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

    }
}
